import instance from '@/utils/request'
import { IAxiosRes } from '.'

/**
 * 获取文章新闻推荐
   基本信息
   Path： /v1_0/articles
   
   Method： GET
   channel_id	是		频道ID
   timestamp	是		时间戳，请求新的推荐数据传当前的时间戳，请求历史推荐传指定的时间戳

   */
export declare type TAtilcle = {
  art_id: string
  aut_id: string
  aut_name: string
  comm_count: 0
  cover: {
    images: string[]
    type: 3 | 1 | 0
  }
  is_top: number
  pubdate: string
  title: string
}
export declare type TAticleRes = {
  pre_timestamp: string
  results: TAtilcle[]
}
export const getArticles = (
  channel_id: number,
  timestamp?: number
): IAxiosRes<TAticleRes> => {
  return instance({
    url: `/articles?channel_id=${channel_id}&timestamp=${timestamp ||
      Date.now()}`
  })
}

/**
 * 获取新闻详情
   基本信息
   Path： /v1_0/articles/:article_id
   Method： GET
   article_id		文章id
 */
export declare type TAtilcleInfo = {
  art_id: string
  attitude: number
  aut_id: string
  aut_name: string
  aut_photo: string
  comm_count: number
  content: string
  is_collected: boolean
  is_followed: boolean
  like_count: number
  pubdate: string
  read_count: number
  title: string
}
export const getArticleById = (article_id: string): IAxiosRes<TAtilcleInfo> => {
  return instance({
    url: `/articles/${article_id}`
  })
}

export function likeArticle (id: string) {
  return instance({
    url: `/article/likings`,
    method: 'post',
    data: {
      target: id
    }
  })
}
export function disLikeArticle (id: string) {
  return instance({
    url: `/article/likings/${id}`,
    method: 'delete'
  })
}

/**
 * 
 * 收藏文章
 * Path： /v1_0/article/collections
   Method： POST
   接口描述：
   target收藏的目标文章id	
 */
export function collectArticle (id: string) {
  return instance({
    url: `/article/collections`,
    method: 'post',
    data: {
      target: id
    }
  })
}

/**
   * 取消收藏文章
     基本信息
     Path： /v1_0/article/collections/:target
     Method： DELETE
*/
export function disCollectArticle (id: string) {
  return instance({
    url: `/article/collections/${id}`,
    method: 'delete'
  })
}

/**
   * 获取评论
     基本信息
     Path： /v1_0/comments
     Method： get
*/
// 评论类型
export type Comment = {
  aut_id: string
  aut_name: string
  aut_photo: string
  com_id: string
  content: string
  is_followed: boolean
  is_liking: boolean
  like_count: number
  pubdate: string
  reply_count: number
}
// 评论的返回值类型
export type CommentRes = {
  end_id: string
  last_id: string
  results: Comment[]
  total_count: number
}
export function getComment (
  type: 'a' | 'c',
  source: string
): IAxiosRes<CommentRes> {
  return instance({
    url: `/comments`,
    method: 'get',
    params: {
      type,
      source
    }
  })
}
// 评论的返回值类型
export type AddCommentRes = { com_id: string; new_obj: Comment; target: string }
// 发表评论
export function addComment (
  target: string,
  content: string
): IAxiosRes<AddCommentRes> {
  return instance({
    url: `/comments`,
    method: 'post',
    data: { target, content }
  })
}
